package com.springboot.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.springboot.pojo.User;
import com.springboot.service.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

//@Controller
//@ResponseBody
@RestController
public class ResponseController2 {
    @Autowired
    private UserService userService;

    @GetMapping("/ajax/req3")
    //@ResponseBody
    public User res3() {
        System.out.println("/ajax/req3 runnging...");
        User user = new User();
        user.setUsername("haohao");
        user.setAge(20);

        return user;
    }


    @GetMapping("/ajax/req2")
    @ResponseBody
    public String res2() {
        User user = new User();
        user.setUsername("haohao");
        user.setAge(19);
        //json转换工具
        ObjectMapper mapper = new ObjectMapper();
        String jsonStr = null;
        try {
            jsonStr = mapper.writeValueAsString(user);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

        return jsonStr;
    }

    @GetMapping("/ajax/req1")
    @ResponseBody
    public String res1() {
        return "{\"username\":\"haohao\",\"age\":18}";
    }


    @PostMapping("/test44")
    public String method1(
            @RequestPart("file") MultipartFile[] file,
            @RequestPart("user") String userJson  // 接收字符串
    ) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        User user = mapper.readValue(userJson, User.class); // 手动反序列化
        System.out.println(user);
        for (MultipartFile multipartFile : file) {
            System.out.println(multipartFile.getOriginalFilename());
        }
        return "method1";
    }

    @PostMapping("/test23")
    public String method2(@RequestBody User userJson  )  {
        System.out.println("userJson = " + userJson);


        List<User> allUsers = null;
      /*  List<User> list = userService.list();
        System.out.println("list = " + list);*/
        // 创建分页对象

       allUsers = userService.getAllUsers();

        System.out.println("allUsers = " + allUsers);
        return "method1";
    }




}
